<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover"/>
<meta name="description" content="Support link connections within a node as well as between nodes."/> 
<link rel="stylesheet" href="../assets/css/style.css"/> 
<!-- Copyright 1998-2021 by Northwoods Software Corporation. -->
<title>Connection Box Nodes</title>
</head>

<body>
  <!-- This top nav is not part of the sample code -->
  <nav id="navTop" class="w-full z-30 top-0 text-white bg-nwoods-primary">
    <div class="w-full container max-w-screen-lg mx-auto flex flex-wrap sm:flex-nowrap items-center justify-between mt-0 py-2">
      <div class="md:pl-4">
        <a class="text-white hover:text-white no-underline hover:no-underline
        font-bold text-2xl lg:text-4xl rounded-lg hover:bg-nwoods-secondary " href="../">
          <h1 class="mb-0 p-1 ">GoJS</h1>
        </a>
      </div>
      <button id="topnavButton" class="rounded-lg sm:hidden focus:outline-none focus:ring" aria-label="Navigation">
        <svg fill="currentColor" viewBox="0 0 20 20" class="w-6 h-6">
          <path id="topnavOpen" fill-rule="evenodd" d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM9 15a1 1 0 011-1h6a1 1 0 110 2h-6a1 1 0 01-1-1z" clip-rule="evenodd"></path>
          <path id="topnavClosed" class="hidden" fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path>
        </svg>
      </button>
      <div id="topnavList" class="hidden lg:text-base sm:block items-center w-auto mt-0 text-white p-0 z-20">
        <ul class="list-reset list-none font-semibold flex justify-end flex-wrap sm:flex-nowrap items-center px-0 pb-0">
          <li class="p-1 sm:p-0"><a class="topnav-link" href="../learn/">Learn</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="../samples/">Samples</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="../intro/">Intro</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="../api/">API</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/products/register.html">Register</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="../download.html">Download</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="https://forum.nwoods.com/c/gojs/11">Forum</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/contact.html"
           target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/contact.html', 'contact');">Contact</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/sales/index.html"
           target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/sales/index.html', 'buy');">Buy</a></li>
        </ul>
      </div>
    </div>
    <hr class="border-b border-gray-600 opacity-50 my-0 py-0" />
  </nav>
  <div class="md:flex flex-col md:flex-row md:min-h-screen w-full max-w-screen-xl mx-auto">
    <div id="navSide" class="flex flex-col w-full md:w-48 text-gray-700 bg-white flex-shrink-0"></div>
    <!-- * * * * * * * * * * * * * -->
    <!-- Start of GoJS sample code -->
    
    <script src="../release/go.js"></script>
    <div class="p-4 w-full">
    <script id="code">
    function init() {
      var $ = go.GraphObject.make;

      myDiagram =
        $(go.Diagram, "myDiagramDiv",
          {
            initialContentAlignment: go.Spot.Center,  // for v1.*
            initialScale: 1.4,
            "undoManager.isEnabled": true,
            "linkingTool.direction": go.LinkingTool.ForwardsOnly,
            "ModelChanged": function(e) {     // just for demonstration purposes,
              if (e.isTransactionFinished) {  // show the model data in the page's TextArea
                document.getElementById("mySavedModel").textContent = e.model.toJson();
              }
            }
          });

      myDiagram.nodeTemplate =
        $(go.Node, "Table",
          new go.Binding("location", "location", go.Point.parse).makeTwoWay(go.Point.stringify),
          {
            selectionObjectName: "BODY",
            linkValidation: function(fromnode, fromport, tonode, toport, link) {
              if (!fromport || !toport) return false;
              if (fromnode === tonode) {
                // inside a node must go from an input port to an output port
                return fromport.portId[0] === "i" && toport.portId[0] === "o";
              } else {
                // between nodes the port colors must match
                if (fromport.fill !== toport.fill) return false;
                // between nodes must go from an output port to an input port
                return fromport.portId[0] === "o" && toport.portId[0] === "i";
              }
              return true;
            }
          },

          $(go.RowColumnDefinition, { column: 1, width: 70 }),
          $(go.Shape,
            {
              name: "BODY",
              row: 0, rowSpan: 99, column: 0, columnSpan: 3, stretch: go.GraphObject.Fill,
              fill: "gray", strokeWidth: 0, margin: new go.Margin(0, 8)
            },
          ),
          $(go.TextBlock,
            { row: 0, columnSpan: 3, margin: new go.Margin(4, 2, 2, 2) },
            new go.Binding("text")),
          $(go.TextBlock,
            { row: 2, columnSpan: 3, margin: new go.Margin(4, 2, 2, 2) },
            new go.Binding("text", "text2")),

          $(go.Panel, "Table",
            new go.Binding("itemArray", "inPorts"),
            {
              row: 1, column: 0,
              defaultSeparatorPadding: new go.Margin(4, 0),
              itemTemplate:  // input ports
                $(go.Panel, "TableRow",
                  { background: "white" },
                  $(go.Shape,
                    {
                      width: 6, height: 6, strokeWidth: 0, margin: new go.Margin(2, 6, 2, 0),
                      fromSpot: go.Spot.Right, toSpot: go.Spot.Left,
                      fromLinkable: true, toLinkable: true,
                      fromLinkableSelfNode: true, toLinkableSelfNode: true, cursor: "pointer"
                    },
                    new go.Binding("portId", "row", function(r) { return "i" + r; }).ofObject(),
                    new go.Binding("fill", "", convertToColor))
                )
            }
          ),
          $(go.Shape,
            {
              row: 1, column: 1, fill: "white", strokeWidth: 0,
              stretch: go.GraphObject.Fill
            }),
          $(go.Panel, "Table",
            new go.Binding("itemArray", "outPorts"),
            {
              row: 1, column: 2,
              defaultSeparatorPadding: new go.Margin(4, 0),
              itemTemplate:  // output ports
                $(go.Panel, "TableRow",
                  { background: "white" },
                  $(go.Shape,
                    {
                      width: 6, height: 6, strokeWidth: 0, margin: new go.Margin(2, 0, 2, 6),
                      fromSpot: go.Spot.Right, toSpot: go.Spot.Left,
                      fromLinkable: true, toLinkable: true,
                      fromLinkableSelfNode: true, toLinkableSelfNode: true, cursor: "pointer"
                    },
                    new go.Binding("portId", "row", function(r) { return "o" + r; }).ofObject(),
                    new go.Binding("fill", "", convertToColor))
                )
            }
          ),
        );

      function convertToColor(n) {
        switch (n) {
          case "r": return "brown";
          case "g": return "olivedrab";
          case "b": return "cornflowerblue";
          default: return "black";
        }
      }

      myDiagram.linkTemplate =
        $(go.Link,
          { relinkableFrom: true, relinkableTo: true },
          $(go.Shape,
            { strokeWidth: 2 },
            new go.Binding("stroke", "fromPort", function(p) {
              return p.fill;
            }).ofObject())
        );

      myDiagram.model = $(go.GraphLinksModel, {
        linkFromPortIdProperty: "fpid",
        linkToPortIdProperty: "tpid",
        copiesArrays: true,
        copiesArrayObjects: true,
        nodeDataArray:
          [
            {
              key: 1, text: "Alpha",
              location: "0 0",
              inPorts: ["r", "r", "r", "g", "b"],
              outPorts: ["r", "g", "g", "b", "b"]
            },
            {
              key: 2, text: "Beta",
              location: "200 -80",
              inPorts: ["r", "r", "g", "g", "b"],
              outPorts: ["r", "r", "g", "g", "b"]
            },
            {
              key: 3, text: "Gamma",
              location: "200 80",
              inPorts: ["r", "r", "g", "g", "b"],
              outPorts: ["r", "r", "g", "g", "b"]
            }
          ],
        linkDataArray:
          [
            { from: 1, fpid: "i0", to: 1, tpid: "o1" },
            { from: 1, fpid: "i1", to: 1, tpid: "o4" },
            { from: 1, fpid: "i2", to: 1, tpid: "o0" },
            { from: 1, fpid: "i3", to: 1, tpid: "o3" },
            { from: 1, fpid: "i4", to: 1, tpid: "o2" },

            { from: 1, fpid: "o0", to: 2, tpid: "i0" },
            { from: 1, fpid: "o2", to: 2, tpid: "i2" },
            { from: 1, fpid: "o2", to: 3, tpid: "i2" },
            { from: 1, fpid: "o3", to: 2, tpid: "i4" },
            { from: 1, fpid: "o3", to: 3, tpid: "i4" },

            { from: 2, fpid: "i0", to: 2, tpid: "o1" },
            { from: 2, fpid: "i2", to: 2, tpid: "o0" },
            { from: 2, fpid: "i4", to: 2, tpid: "o2" },
            { from: 3, fpid: "i2", to: 3, tpid: "o3" },
            { from: 3, fpid: "i1", to: 3, tpid: "o1" }
          ]
        });
    }
    window.addEventListener('DOMContentLoaded', init);
  </script>

<div id="sample">
  <div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:600px"></div>
  <p>
    You can draw new links between ports, both between nodes and within a node.
    Between nodes, the link validation predicate requires new link connections to connect ports of the same color.
    However, within a node, links may connect ports of different colors.
  </p>
  <textarea id="mySavedModel" style="width:100%;height:250px"></textarea>
</div>
    </div>
    <!-- * * * * * * * * * * * * * -->
    <!--  End of GoJS sample code  -->
  </div>
</body>
<!--  This script is part of the gojs.net website, and is not needed to run the sample -->
<script src="../assets/js/goSamples.js"></script>
</html>
